package com.qianying.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianying.comment.CommonResult;
import com.qianying.entity.WorkType;
import com.qianying.service.WorkTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 工种类型控制器
 * 提供工种类型的HTTP接口，处理前端请求
 */
@RestController
@RequestMapping("/admin/workType")
@Tag(name = "师傅工种（工种类型管理）", description = "工种类型信息的增删改查接口")
public class WorkTypeController {

    @Autowired
    private WorkTypeService workTypeService;

    /**
     * 分页查询工种类型列表
     * @param current 页码
     * @param size 每页条数
     * @param typeName 工种类型名称（模糊查询）
     * @return 分页结果
     */
    @GetMapping("/getPage")
    @Operation(
            summary = "分页查询工种类型列表",
            description = "支持按工种类型名称模糊查询，返回分页数据",
            parameters = {
                    @Parameter(name = "current", description = "页码", example = "1"),
                    @Parameter(name = "size", description = "每页条数", example = "10"),
                    @Parameter(name = "typeName", description = "工种类型名称（模糊查询）", example = "电工")
            }
    )
    public CommonResult<IPage<WorkType>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String typeName) {
        Page<WorkType> page = new Page<>(current, size);
        return workTypeService.getPage(page, typeName);
    }

    /**
     * 根据ID查询工种类型详情
     * @param id 主键ID
     * @return 工种类型详情
     */
    @GetMapping("/getById")
    @Operation(
            summary = "查询工种类型详情",
            description = "通过ID获取工种类型的完整信息",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1")
            }
    )
    public CommonResult<WorkType> getById(@RequestParam Long id) {
        return workTypeService.getById(id);
    }

    /**
     * 新增工种类型
     * @param workType 工种类型信息（ID需为空）
     * @return 操作结果
     */
    @PostMapping("/saveType")
    @Operation(
            summary = "新增工种类型",
            description = "创建新的工种类型记录，工种类型名称为必填项，ID必须为空",
            parameters = {
                    @Parameter(name = "typeName", description = "工种类型名称", required = true, example = "电工"),
                    @Parameter(name = "typeCode", description = "工种类型代号", example = "ELECTRICIAN"),
                    @Parameter(name = "certificate", description = "是否需要证书0 不需要，1 需要", example = "1"),
                    @Parameter(name = "description", description = "工种类型说明", example = "负责电气系统的安装、维修和维护"),
            }
    )
    public CommonResult<?> saveType(@RequestBody WorkType workType) {
        return workTypeService.saveType(workType);
    }

    /**
     * 更新工种类型
     * @param workType 工种类型信息（需包含ID）
     * @return 操作结果
     */
    @PostMapping("/updateType")
    @Operation(
            summary = "更新工种类型",
            description = "修改已有工种类型信息，必须包含ID和工种类型名称",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1"),
                    @Parameter(name = "typeName", description = "工种类型名称", required = true, example = "电工"),
                    @Parameter(name = "certificate", description = "是否需要证书0 不需要，1 需要", example = "1"),
                    @Parameter(name = "typeCode", description = "工种类型代号", example = "ELECTRICIAN"),
                    @Parameter(name = "description", description = "工种类型说明", example = "负责电气系统的安装、维修和维护")
            }
    )
    public CommonResult<?> updateType(@RequestBody WorkType workType) {
        return workTypeService.updateType(workType);
    }

    /**
     * 根据ID删除工种类型
     * @param id 主键ID
     * @return 操作结果
     */
    @PostMapping("/removeById")
    @Operation(
            summary = "删除工种类型",
            description = "通过ID删除指定的工种类型记录",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1")
            }
    )
    public CommonResult<?> removeById(@RequestParam Long id) {
        return workTypeService.removeById(id);
    }

    /**
     * 查询所有工种类型（不分页）
     * @return 工种类型列表
     */
    @GetMapping("/getAll")
    @Operation(
            summary = "查询所有工种类型",
            description = "返回所有工种类型列表，适用于下拉选择等场景"
    )
    public CommonResult<List<WorkType>> getAll() {
        return workTypeService.getAll();
    }
}